package product.servlet;

import product.bean.Product;
import product.service.impl.productServiceimpl;
import product.service.productService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by Administrator on 2018/11/28.
 */
@WebServlet("/product")
public class pruductservlet extends HttpServlet {
    productService productService=new productServiceimpl();
    protected void getBookList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Product> bookList = productService.getBookList();
        req.setAttribute("list1",bookList);
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/jsp/product.jsp");
        requestDispatcher.forward(req,resp);

    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.修改请求的字符集编码
        req.setCharacterEncoding("UTF-8");
        //2,修改响应的字符集编码和类型
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        //3,获得处理当前请求的方法的名称
        String methodname=req.getParameter("methodname");
        try {
            Method method=this.getClass().getDeclaredMethod(methodname,HttpServletRequest.class,HttpServletResponse.class);
            method.setAccessible(true);
            method.invoke(this,req,resp);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
